package org.layui.servlet;

import org.layui.dao.UserDao;
import org.layui.dao.impl.UserDaoImpl;
import org.layui.entity.User;
import org.layui.utils.JwtUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/api/admin")
public class AdminServlet extends BaseServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取客户端发送过来的token -> "Bearer xxxxxxxxxxxxxxxxxxxxxx"
        String token = req.getHeader(JwtUtil.TOKEN_HEADER);
        String jwt = token.replace(JwtUtil.TOKEN_PREFIX, "");

        // 获取payload信息
        String username = JwtUtil.getPayload(jwt, "username", String.class);

        // 查询当前登录用户信息
        UserDao userDao = new UserDaoImpl();
        User user = userDao.selectUser(username);
        user.setPassword("");

        // 响应客户端
        print(resp, successJson(user));
    }
}
